﻿(function() {

var Version = {
  maps: "maps",
  ditu: "ditu",
  gadget : "gadget",
  mapplet : "mapplet"
};
var Active = {
  tab : null,
  mapType : null
};
var Default = {
  version:null,
  lang:null,
  hasEarth : false,
  baseUrl:"http://2008summergames.googlecode.com/svn/com/",
  imageUrl: "http://mw1.google.com/mw-earth-vectordb/geographic_web/summer_games08/mapimages/",
  dataUrl:"",
  map : null,
  center : new GLatLng(1.406109, -3.515625),
  level : 1,
  mapTypes : {},
  key : {
    medal : "medal",
    event : "event",
    venue : "venue",
    star : "star"
  },
  tabs : {},
  isDebug : false,
  earthObj: null
};
window.Default = Default;

function GoMgr(mid, prefs) {
  Go.showMessage(Go.getLocalLabel("loading"));
  this.mid = mid;
  this.prefs = prefs;
  Default.version = this.getVersion();
  Default.lang = this.getLang();
  this.map = Default.map = this.initMap();
  Default.dataUrl = "http://www.google.com/ig/olympics?";
  this.initPanel();
  this.addMapTypeButton();
}
window.GoMgr = GoMgr;

GoMgr.prototype.initMap = function() {
  if(document.all)
  document.execCommand("BackgroundImageCache", false, true);
  var div = Go.$("mapcontent");
  var map = new GMap2(div, {mapTypes:[G_NORMAL_MAP, G_HYBRID_MAP]});
  Default.map = map;
  map.setCenter(Default.center, Default.level);
  map.enableScrollWheelZoom();
  map.addControl(new GLargeMapControl());
  var mgr = VenueMgr.getInstance();
  GEvent.addListener(map, "maptypechanged", GEvent.callback(mgr, mgr.coordinateOffset));
  return map;
}
GoMgr.prototype.initPanel = function() {
  Go.$h("script",Default.baseUrl + "js/medal.js");
  Go.$h("script",Default.baseUrl + "js/event.js")
  Go.$h("script",Default.baseUrl + "js/venue.js")
//  Go.$h("script",Default.baseUrl + "js/star.js")
  this.fold = true;
  GEvent.addDomListener(Go.$("panelFolder"), "click", GEvent.callback(this, this.expandMap));
  Go.hideMessage();
}
GoMgr.prototype.getLang = function(){
  return this.prefs.getLang();
}
GoMgr.prototype.getVersion = function(){
  return Go.getUrlParameter('ver', 4) || Version.maps;
}
GoMgr.prototype.getCountry = function(){
  return pref.getCountry();
}
GoMgr.prototype.addMapTypeButton = function(){
  function createDom(id, html, style){
    return Go.$n("div", null, "nor", html, {id:id, style:style});
  }
  
  var dom = createDom("hybridType", Go.getLocalLabel("satellite"), "margin-right:10px"); 
  Default.mapTypes["hybrid"] = new MapTypeControl("hybridType", G_HYBRID_MAP, dom);
  dom = createDom("normalType", Go.getLocalLabel("map"), "");
  Default.mapTypes["normal"] = new MapTypeControl("normalType", G_NORMAL_MAP, dom);
}
function getEarthInstanceCB(object){
    if(!object){
       return;
    }
    Default.earthObj = object;
    if(object) object.getLayerRoot().enableLayerById(object.LAYER_BORDERS, true);
    var placemarks = Default.earthObj.getFeatures().getChildNodes();
    var len = placemarks.getLength();
    for(var i = 0; i < len; i++){
        var placemark = placemarks.item(i);
        var venues = DVenue.item;
        for(var index in venues) if(venues.hasOwnProperty(index)){
            var venue = venues[index];
            if(venue.label == placemark.getName()){
                venue["placemark"] = placemark;
                if(venue.marker.isHidden()){
                    placemark.setVisibility(false);
                }
                break;
            }
        }
    }
}
// expand or fold the map content.
GoMgr.prototype.expandMap = function(){
  var obj = Go.$("panelFolder");
  if(this.fold){
    Go.$("leftcontent").style.display = "none";
    Go.$("rightcontent").style.width = "760px";
    this.fold = false;
    obj.style.backgroundImage = "url("+Default.imageUrl + "icons/expand.gif)";
  } else {
    Go.$("leftcontent").style.display = "";
    Go.$("rightcontent").style.width = "457px";
    this.fold = true;
    obj.style.backgroundImage = "url("+Default.imageUrl + "icons/fold.gif)";
  }  
  this.map.savePosition();
  this.map.checkResize();
  this.map.returnToSavedPosition();
};


/**
 *  Super class of all tabs
 */
var GoTab = function(key, defaultMap, needEarth){
  this.key = key;
  this.needEarth = needEarth;
  this.defaultMap = defaultMap || {};
  this.mapStatus = {};
  this.tabId = key + "_tab";
  this.panelId = key + "_panel";
  this.loaded = false;
  this.initialize();
}
window.GoTab = GoTab;
GoTab.prototype.initialize = function(){
  Go.$(this.panelId).style.display = "none";
  GEvent.addDomListener(Go.$(this.tabId), "click", GEvent.callback(this, this.active));
}
GoTab.prototype.active = function(){
  if(Active.tab && Active.tab.key == this.key) return;
  var oldTab = Active.tab;
  if(document.all && oldTab && oldTab.calendar && (!oldTab.calendar.isHide())){
    oldTab.calendar.hide();
    setTimeout(new deactiveClosure(oldTab, this.panelId), 250);
  }
  else{
    if(oldTab){
      oldTab.deactive();
    }
    Go.$(this.panelId).style.display = "";
  }

  Go.$(this.tabId).className = "running";
  
  
  this.restoreMap();
  Active.tab = this;
  if(!this.loaded){
    this.load();
    this.loaded = true;
    if(!Default.hasEarth){
         if(navigator.platform.match(/win/i) && navigator.platform.match(/win/i).length != 0 && (window.ActiveXObject || document.getBoxObjectFor)){
            function createDom(id, html, style){
                return Go.$n("div", null, "nor", html, {id:id, style:style});
            }
            if (window.google.loader && window.google.loader.ApiKey == "notsupplied") {
                window.google.loader.ApiKey = "ABQIAAAA91OZMiOe8VfhdtstxQrBBxSABNjtVb8iFqV9RjCy_Ut9GGD7HxSE8bPZLEHlxWQ47LNBCcFG8Op5zQ";
            }
            Default.map.addMapType(G_SATELLITE_3D_MAP);
            var dom = createDom("earthType", Go.getLocalLabel("earth")+'<span style="color:red;font-size:8px;vertical-align:top">&nbsp;'+Go.getLocalLabel("new")+'</span>', "");
            Default.mapTypes["earth"] = new MapTypeControl("earthType", G_SATELLITE_3D_MAP, dom);
            Default.mapTypes["earth"].hide();
            Default.hasEarth = true;
           // if (google.earth.isInstalled()) {
           //     Default.map.getEarthInstance(getEarthInstanceCB);
          //  }
        }
    }
  } else {
  	this.reload();
  }
}

function deactiveClosure(tab, newTabId){
    return function(){
        tab.deactive();
        Go.$(newTabId).style.display = "";
    }
}

GoTab.prototype.deactive = function(){

  Go.$(this.tabId).className = "tab";
//  Default.map.closeInfoWindow();
  this.saveStatus();
  this.clear();
  Go.$(this.panelId).style.display = "none";
}
GoTab.prototype.load = function(){}  // Abstract method
GoTab.prototype.reload = function(){}  // Abstract method
GoTab.prototype.clear = function(){}  // Abstract method
GoTab.prototype.saveStatus = function(){
  var map = Default.map;
  var center= map.getCenter();
  var level = map.getZoom();
  var type = map.getCurrentMapType();
  this.mapStatus = {
    center :　center, level : level, type : type
  }
}
GoTab.prototype.restoreMap = function(){
  var map = Default.map;
  map.setCenter(this.mapStatus.center || this.defaultMap.center || Default.center);
  map.setZoom(this.mapStatus.level || this.defaultMap.level || Default.level);
  this.checkMapTypeButton();
}
GoTab.prototype.resizeMap = function(locations) {
  var map = Default.map;
  var space = new GLatLngBounds();
  for(var i in locations){
    var marker = DVenue.item[i].marker;
    space.extend(marker.getPoint());
  }
  if (!space.isEmpty()) {
    var center = space.getCenter();
    var level = map.getBoundsZoomLevel(space);
    map.setCenter(center, level);
  }
}
GoTab.prototype.checkMapTypeButton = function(key){
  if (Default.hasEarth){
  	if(key) var needEarth = Default.tabs[key].needEarth;
    else var needEarth = this.needEarth;
    if(needEarth){
      Default.mapTypes["earth"].show();
    } else {
      Default.mapTypes["earth"].hide();
    }
  }
  var type = this.mapStatus.type || this.defaultMap.type || G_NORMAL_MAP;
  var key = "";
  switch(type){
    case G_HYBRID_MAP:
      key = "hybrid";
      break;
    case G_NORMAL_MAP:
      key = "normal";
      break;
    case G_SATELLITE_3D_MAP:
      key = "earth";
      break;
    default:
      alert("unsport map type"+type);
  }
  if(Default.map.getCurrentMapType() == G_SATELLITE_3D_MAP && key == "earth"){
    Default.mapTypes["normal"].active();
  }
  else if(type == Default.map.getCurrentMapType()){
    Default.map.closeInfoWindow();
  }
  Default.mapTypes[key].active();
}
GoTab.prototype.checkFavo = function(id, key){
  var favos = Go.getFavo(key);
  for(var i = 0; i < favos.length; i++){
    if(favos[i] == id) return true;
  }
  return false;
}
GoTab.prototype.genStarIcon = function(id, key, isFromStarTab, node, specStyle){
  var icon = this.checkFavo[id, key] ? Default.imageUrl + "icons/starred.gif" : Default.imageUrl + "icons/star.gif";
  var id = isFromStarTab ? "star_" + id + "_" + key + "_t" : "star_" + id + "_" + key;
  var star = Go.$n("img", node, "star-icon", null, {src : icon, id : id, style : specStyle});
  GEvent.addDomListener(star, "click", GEvent.callback(this, this.onStarClick));
  return star;
}
GoTab.prototype.onStarClick = function(e){
  var e = e || window.event;
  Go.stopPropagation(e);
  var star = e.target || e.srcElement;
  var result = star.id.split("_");
  var id = result[1];
  var key = result[2];
  if(this.checkFavo(id, key)){
    star.src = Default.imageUrl + "icons/star.gif";
    Go.removeFavo(id, key);
    if(result.length == 4){
      var node = star.parentNode;
      node.parentNode.removeChild(node);
      var starTab = Default.tabs[Default.key.star];
      starTab.removeMarker(id, key);
      if(node.parentNode.childNodes.length == 0){
        starTab.addNothingHint(node.parentNode, key);
      }
    }
    Go.showMessage(Go.getLocalLabel("remove_msg"));
  } else {
    star.src = Default.imageUrl + "icons/starred.gif";
    Go.addFavo(id, key);
    Go.showMessage(Go.getLocalLabel("add_msg"));
  }
}
GoTab.prototype.openInfowidow = function(sn){}  // Abstract method
/**
 * The manager class to manage venue data and it's mark, it's a singlton class.
 */
var VenueMgr = function(){
  this.mappingKey = {
    "National Stadium":"ns",
    "National Indoor Stadium": "nis",
    "National Aquatics Center": "nac",
    "Fencing Hall" :"fh",
    "Olympic Green Archery Field": "af",
    "Olympic Green Tennis Center": "tc",
    "Olympic Green Hockey Stadium" :"hf",     
    "OSC Stadium": "cs",
    "OSC Gymnasium" :"cg",
    "Yingdong Natatorium" :"ytn",
    "Beijing Shooting Range CTF": "src",
    "Beijing Shooting Range Hall": "srh",
    "BMX Venue" :"bmx",
    "Laoshan Velodrome" :"lv",
    "Laoshan Mountain Bike Course": "bmc",
    "Road Cycling Course": "brc",
    "Fengtai Softball Field": "sf",
    "Olympic Basketball Gymnasium": "wis",
    "Wukesong Baseball Field" :"bf",
    "SY Rowing-Canoeing Park": "rcp",
    "Triathlon Venue": "tv",
    "Capital Gymnasium" :"cis",
    "BUAA Gymnasium": "buaa",
    "PKU Gymnasium": "pu",
    "CAU Gymnasium": "cau",
    "USTB Gymnasium": "ustb",
    "BIT Gymnasium" :"bit",
    "BJUT Gymnasium": "but",
    "Workers' Stadium": "ws",
    "Workers' Gymnasium" :"wia",
    "Chaoyang Park BV Ground" :"bvg",
    "Qingdao Olympic Sailing Center": "qd",
    "HK Equestrian (Beas River)": "br", 
    "HK Equestrian(Shatin)": "st",
    "TJ Olympic Center Stadium": "tj",
    "QHD Stadium" :"qhd",
    "Shenyang Olympic Stadium": "sy",
    "Shanghai Stadium": "sh"
  };
  this.videoId = {
    "bit": "1jald5QZzdU",
    "wis": "hGE3qKaOx_I",
    "af": "V4HkCDTzcaU",
    "hf": "G84i1myi5MI",
    "tc": "tic0N5PYs9I",
    "ustb": "7OZj6qMNkoI",
    "src": "Oyb6nZ6R6CE",
    "srh": "1HAn53mUIDI",
    "but": "EyZd5ffnfYo",
    "wia": "NiKoK1gVA84",
    "ws": "QJHCbGLXi-s",
    "bf": "oeYPsSoxx_o",
    "cis": "2N5wdRosqFg",
    "bvg": "kN_jIaFMIPI",
    "cau": "gMPKlz0yVfQ",
    "fh": "tPzVh7tSay8",
    "sf": "ToHsEDWJyV4",
    "bmx": "RIzjK2HYwE",
    "bmc": "fAtki_tv1To",
    "lv": "kSlc3_ylHuY",
    "nac": "4EYVuwwSAoA",
    "nis": "t0Zzd-In3u0",
    "ns": "-9SZkGos-Ew",
    "cg": "xHOqvKt3KjE",
    "cs": "s_jNgmK-QXU",
    "pu": "Ocga7wcS4jQ",
    "brc": "x1n2zOpyPj8",
    "rcp": "ZlK-iSHNl2M",
    "tv": "h_lwwwOS8IM",
    "ytn": "ldLRL2EsIcU"
  }
  this.initialize();
}
window.VenueMgr = VenueMgr;
var venueMgr = null;
VenueMgr.getInstance = function(){
  if(!venueMgr)venueMgr = new VenueMgr();
  return venueMgr;
}
VenueMgr.prototype.initialize = function(){
  var data = DVenue.item;
  var prefix = DVenue.prefix;
  var map = Default.map;
  for (var i in data) {
    var venue = data[i];
    var point = new GLatLng(venue.pt[1], venue.pt[0]);
    if (!venue.label) venue.label = Go.getLocalLabel(i, prefix);
    if (!venue.marker){
      var icon = this.createIcon(Default.imageUrl + "icons/stadium-32-small.gif")
      venue.marker = new GMarker(point, {icon:icon, title:venue.label});
      map.addOverlay(venue.marker);
      venue.marker.hide();
    }
    GEvent.addListener(venue.marker, 'mouseover', GEvent.callbackArgs(this, this.highlightLoca, i));
    GEvent.addListener(venue.marker, 'click', GEvent.callbackArgs(this, this.activeLoca, i, true));
    GEvent.addListener(venue.marker, 'mouseout', GEvent.callbackArgs(this, this.unHighlightLoca, i));
    GEvent.addListener(venue.marker, 'infowindowclose', GEvent.callbackArgs(this, this.unHighlightLoca, i));
  }
  GEvent.addListener(map, "maptypechanged", GEvent.callback(this, this.coordinateOffset));
}
VenueMgr.prototype.getVideoId = function(sn){
  return this.videoId[sn];
}
VenueMgr.prototype.findNodeId = function(sn){
  var nodeId = null;
  var key = Active.tab.key;
  if(key == Default.key.venue){
    nodeId = sn+"_"+key; 
  } else if(key == Default.key.star && Active.tab.curKey == Default.key.venue){
    nodeId = sn+"_"+key + "_t";
  } else {
    // marker is relate with event, can not highlight only one event node
  }
  return nodeId;
}
VenueMgr.prototype.findVenueSn = function(nodeId){
  var sn = null;
  var key = Active.tab.key;
  if(key == Default.key.venue || (key == Default.key.star && Active.tab.curKey == Default.key.venue)){
    sn = nodeId.split("_")[0];
  } else {
    var eventId = nodeId.split("_")[0];
    sn = DEvent[eventId].sn;
  }
  return sn;
}
VenueMgr.prototype.highlightLoca = function(nodeId){
  if(nodeId.split("_").length == 1){  // From marker
    this.highMarker(nodeId);
    nodeId = this.findNodeId(nodeId);
    if(nodeId){
      this.highNode(nodeId);
      return nodeId;
    } else {
      return null;  // return event list of this marker related?
    }
  } else {
    this.highNode(nodeId);
    var sn = this.findVenueSn(nodeId);
    this.highMarker(sn);
    return nodeId;
  }
}
VenueMgr.prototype.unHighlightLoca = function(nodeId){
  if(nodeId.split("_").length == 1){  // From marker
    this.unHighMarker(nodeId);
    nodeId = this.findNodeId(nodeId);
    if(nodeId) this.unHighNode(nodeId);
  } else {
    this.unHighNode(nodeId);
    var sn = this.findVenueSn(nodeId);
    this.unHighMarker(sn);
  }
}
VenueMgr.prototype.activeLoca = function(nodeId, isNoZoom){
  var true_nodeId = this.highlightLoca(nodeId);
  if(Active.nodeId && Active.nodeId != true_nodeId){
    this.unHighlightLoca(true_nodeId);
  }
  var sn = null;
  if(nodeId.split("_").length == 1){  //  From marker
  	sn = nodeId;
  } else {
  	sn = this.findVenueSn(nodeId);
  }
  if(!isNoZoom){
    var target = DVenue.item[sn];
    target = new GLatLng(target.pt[1], target.pt[0]);
    Default.map.setCenter(target, 17); 
  }
  Active.tab.openInfowindow(sn, true_nodeId);
}
VenueMgr.prototype.highMarker = function(sn){
  var loca = DVenue.item[sn];
  var marker = loca.marker;
  marker.setImage(Default.imageUrl + "icons/stadium-32-large.gif");
}
VenueMgr.prototype.unHighMarker = function(sn){
  var loca = DVenue.item[sn];
  var marker = loca.marker;
  marker.setImage(Default.imageUrl + "icons/stadium-32-small.gif");
}
VenueMgr.prototype.highNode = function(nodeId){
  Go.$(nodeId).className = "highlight";
  Go.autoScroll(Go.$(nodeId));
}
VenueMgr.prototype.unHighNode = function(nodeId){
  Go.$(nodeId).className = "un-highlight";
}
VenueMgr.prototype.createIcon = function(url, isLarge){
  var newicon = new GIcon();
  newicon.image = url;
  newicon.iconSize = new GSize(32,32);
  newicon.iconAnchor = new GPoint(16, 16);
  newicon.infoWindowAnchor = new GPoint(16, 0);
  return newicon;
}

VenueMgr.prototype.getMappingKey = function(name){
  return this.mappingKey[name];
}
VenueMgr.prototype.coordinateOffset = function(){
  var map = Default.map;
  var data = DVenue.item;
  var type = map.getCurrentMapType();
  if(type != G_SATELLITE_3D_MAP)
    map.closeInfoWindow();
  if(type == G_SATELLITE_3D_MAP || type == G_HYBRID_MAP){
    for(var sn in data){
      var loca = data[sn];
      var point = new GLatLng(loca.sa_pt[1], loca.sa_pt[0]);
      loca.marker.setLatLng(point);
      if(loca.placemark){
        var point2 = Default.earthObj.createPoint('');
        point2.setLatLng(loca.sa_pt[1], loca.sa_pt[0]);
        loca.placemark.setGeometry(point2);
      }
    }
  }else{
    for(var sn in data){
      var loca = data[sn];
      var point = new GLatLng(loca.pt[1], loca.pt[0]);
      loca.marker.setLatLng(point);
    }
  }
}

/**
 *  Namespace for global functions.
 */
var Go = {};
window.Go = Go;
Go.$ = function(id) {
  return document.getElementById(id);
}
Go.$$ = function(name){
   return document.getElementsByTagName(name);
}
Go.$n = function(tag, parent, style, inner, attributes) {
  var el = document.createElement(tag);
  el.className = style || "";
  if (inner) el.innerHTML = inner;
  if (parent) parent.appendChild(el);
  if (attributes){
    for (var nm in attributes){
      el.setAttribute(nm, attributes[nm])
    }
  }
  return el;
}
Go.refreshPlacemark = function(marks, show){
    return function(){
        for(var i in marks) if(marks.hasOwnProperty(i)){
            var loca = DVenue.item[i];
            if(loca.placemark){
                loca.placemark.setVisibility(show);
            }
        }
    }
}
Go.$t = function(text, parent) {
  var el = document.createTextNode(text);
  if (parent) {
    parent.appendChild(el);
  }
  return el;
}
Go.$0 = function(parent) {
  var el = Go.$n("div", parent, "close-icon", "X");
  GEvent.addDomListener(el, "click", function() {
    el.parentNode.style.display = "none";
  });
}
Go.$h = function(tag, url) {
  var head = Go.$$("head")[0] || Go.$n("head", document.body.parentNode);
  switch (tag) {
    case "script" : 
      Go.$n(tag, head, null, null, {"type":"","src":url});
      break;
    case "css" :
      Go.$n(tag, head, null, null, {"type":"text/css", "href":url, "rel":"stylesheet"})
      break;
    default :
      // Only for debug
      alert("Don't support this tag: " + tag);
      head.removeChild(head.lastChild);
      return;
  }
}
Go.stopPropagation = function(e){
  var e = e || window.event; 
  e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;
}
Go.getUrlParameter = function(key, length){
  var query = window.location.search;
  key += '=';
  var index = query.indexOf( key );
  if (index != -1) {
    var r = query.substr(parseInt(index)+ parseInt(key.length), length);
    return r;
  }
  return null;
}
Go.autoScroll = function(node){
  var container = node.parentNode;
  var offset = node.offsetHeight;
  var topPosi = container.offsetTop;
  var bottomPosi = topPosi + container.offsetHeight;
  var curPosi = node.offsetTop - container.scrollTop;
  if( (curPosi + offset) > bottomPosi){  // Under bottom
    container.scrollTop = container.scrollTop + ((curPosi + offset) - bottomPosi);
  } else if(curPosi < topPosi){
    container.scrollTop = container.scrollTop - (topPosi - curPosi);
  }
}

Go.getLocalLabel = function(key, prefix) {
  var key = prefix ? prefix + "-" + key : key;
  return prefs.getMsg(key) || key;
}
Go.getLocalDate = function(day){
	return Go.getLocalLabel("Aug"+day);
}
Go.methodCallback = function(object, method) {
  return function() {
    return method.apply(object, arguments);
  }
}
Go.addFavo = function(value, type){
  var key = type+"_favo";
  var old = prefs.getString(key);
  value = old == "" ? value : old + "|" + value;
  prefs.set(key, value);
};
Go.getFavo = function(type){
  var value = prefs.getString(type+"_favo");
  if(value == null || value ==""){
    return [];
  } else {
    return value.split("|");
  }
};
Go.removeFavo = function(value, type){
  var evts = prefs.getString(type+"_favo");
  if(evts != null){
    evts = evts.replace(new RegExp("|" + value), "");
    evts = evts.replace(new RegExp(value + "|"), "");
    evts = evts.replace(new RegExp(value), "");
  }
  prefs.set(type+"_favo", evts);
};
var timerId = null;
var msgOpacity = 1;
// show a auto hide message on screen.
Go.showMessage = function(mess){
    var divid = Go.$("mess");
    divid.innerHTML = mess;
    if(timerId != null){
        clearTimeout(timerId);
    }
    if (window.ActiveXObject) divid.style.filter = '';
    divid.style.opacity = 1;
    msgOpacity = 1;
    timerId = setTimeout("Go.hideMessage()", 2000);
};
Go.hideMessage = function(){
    var divid = Go.$("mess");
    if(msgOpacity > 0){
        msgOpacity -= 0.1;
        if(window.ActiveXObject){
            divid.style.filter = 'alpha(opacity=' + msgOpacity * 100 + ')';
        }
        divid.style.opacity = msgOpacity;
        timerId = setTimeout("Go.hideMessage()", 50);
    } else {
        divid.innerHTML = "";
        clearTimeout(timerId);
    }
};
Go.loadData = function(url, callback, dateKey_opt){
  var gotData = new Go.getData(callback, url, 2);
  _IG_FetchContent(url, gotData, {refreshInterval: 600});
}

Go.getData = function(callback, url, count){
    return function(opt_response){
        if(!opt_response){
            if(count == 0){
                callback.apply(null, [opt_response]);
                return;
            }
            count--;
            var gotData = new Go.getData(callback, url, count);
            _IG_FetchContent(url, gotData, {refreshInterval: 5});
        }
        else{
            callback.apply(null, [opt_response]);
        }
    }
}
Go.changeTab = function(index, length){
  for(var i = 1; i <= length; i++){
    if(i == index){
      $("tab" + i).className = "info-running";
      $("content" + i).style.display = "";
    }else{
      $("tab" + i).className = "info-tab";
      $("content" + i).style.display = "none";
    }
  }
}


function MapTypeControl(id, type, dom, isActive){
  this.id = id;
  this.type = type;
  this.initialize(dom);
  if(isActive) this.active();
}
MapTypeControl.prototype.initialize = function(dom){
  var parent = Go.$("mapbanner");
  var element = Go.$n("div",parent,"MyTypeBut");
  element.appendChild(dom);
  GEvent.addDomListener(element, "click", GEvent.callback(this, this.active));
}
MapTypeControl.prototype.active = function(){
  if(Active.mapType == this) return;
  Go.$(this.id).className = "run";
  if(this.type == G_SATELLITE_3D_MAP){
    Default.map.getEarthInstance(getEarthInstanceCB);
    Default.map.closeInfoWindow();
  }
  Default.map.setMapType(this.type);
  if(Active.mapType) Active.mapType.deactive();
  Active.mapType = this;
}
MapTypeControl.prototype.deactive = function(){
  Go.$(this.id).className = "nor";
}
MapTypeControl.prototype.show = function(){
  Go.$(this.id).parentNode.style.display = "";
};
MapTypeControl.prototype.hide = function(){
  Go.$(this.id).parentNode.style.display = "none";
}

})();

/*****************************************************************/
/********** BEGIN: Sprite icons implementation *******************/
/*****************************************************************/

function MakeImage(iconMap, iconKey, iconWidth, iconHeight, imgColumns, imageUrl, asHtml, styleText) {
  // fix bug about the short name is match. venue's sports and sports in icons.
  if(iconKey == "WR")
    iconKey = "WF";
  if(iconKey == "EQ")
    iconKey = "EC";
  var index = iconMap[iconKey];
  var x = -iconWidth * Math.floor(index % imgColumns);;
  var y = -iconHeight * Math.floor(index / imgColumns); 
  if(!asHtml){
    var icon = document.createElement('div');
    //icon.style.display = "-moz-inline-box";
    icon.style.width = iconWidth + 'px';
    icon.style.height = iconHeight + 'px';
    icon.style.backgroundImage = "url('" + imageUrl + "')";
    try{
        icon.style.backgroundPosition = x + 'px ' + y + 'px';
    }
    catch(e){
      //  alert(imgColumns + "," + iconKey);
    }
    return icon;
  }else{
    styleText = styleText || '';
    var html = '<div style="'//display:-moz-inline-box;'
        + 'width:' + iconWidth + 'px;height:' + iconHeight
        + 'px;background-image:url(\'' + imageUrl + '\');'
        + 'background-position:' + x + 'px ' + y + 'px' + ';'+styleText+'">&nbsp;</div>';
    return html;
  }
}

function FlagIconFac() {
  FlagIconFac.COUNTRIES_ICON_MAP = {};
  for (var i = 0; i < FlagIconFac.COUNTRIES.length; i++) {
    FlagIconFac.COUNTRIES_ICON_MAP[FlagIconFac.COUNTRIES[i]] = i;
  }
}
FlagIconFac.COUNTRIES = ['AFG', 'AHO', 'ALB', 'ALG', 'AND', 'ANG', 'ANT', 'ARG',
  'ARM', 'ARU', 'ASA', 'AUS', 'AUT', 'AZE', 'BAH', 'BAN', 'BAR', 'BDI', 'BEL',
  'BEN', 'BER', 'BHU', 'BIH', 'BIZ', 'BLR', 'BOL', 'BOT', 'BRA', 'BRN', 'BRU',
  'BUL', 'BUR', 'CAF', 'CAM', 'CAN', 'CAY', 'CGO', 'CHA', 'CHI', 'CHN', 'CIV',
  'CMR', 'COD', 'COK', 'COL', 'COM', 'CPV', 'CRC', 'CRO', 'CUB', 'CYP', 'CZE',
  'DEN', 'DJI', 'DMA', 'DOM', 'ECU', 'EGY', 'ERI', 'ESA', 'ESP', 'EST', 'ETH',
  'FIJ', 'FIN', 'FRA', 'FSM', 'GAB', 'GAM', 'GBR', 'GBS', 'GEO', 'GEQ', 'GER',
  'GHA', 'GRE', 'GRN', 'GUA', 'GUI', 'GUM', 'GUY', 'HAI', 'HKG', 'HON', 'HUN',
  'INA', 'IND', 'IRI', 'IRL', 'IRQ', 'ISL', 'ISR', 'ISV', 'ITA', 'IVB', 'JAM',
  'JOR', 'JPN', 'KAZ', 'KEN', 'KGZ', 'KIR', 'KOR', 'KSA', 'KUW', 'LAO', 'LAT',
  'LBA', 'LBR', 'LCA', 'LES', 'LIB', 'LIE', 'LTU', 'LUX', 'MAD', 'MAR', 'MAS',
  'MAW', 'MDA', 'MDV', 'MEX', 'MGL', 'MHL', 'MKD', 'MLI', 'MLT', 'MNE', 'MON',
  'MOZ', 'MRI', 'MTN', 'MYA', 'NAM', 'NCA', 'NED', 'NEP', 'NGR', 'NIG', 'NOR',
  'NRU', 'NZL', 'OMA', 'PAK', 'PAN', 'PAR', 'PER', 'PHI', 'PLE', 'PLW', 'PNG',
  'POL', 'POR', 'PRK', 'PUR', 'QAT', 'ROU', 'RSA', 'RUS', 'RWA', 'SAM', 'SEN',
  'SEY', 'SIN', 'SKN', 'SLE', 'SLO', 'SMR', 'SOL', 'SOM', 'SRB', 'SRI', 'STP',
  'SUD', 'SUI', 'SUR', 'SVK', 'SWE', 'SWZ', 'SYR', 'TAN', 'TGA', 'THA', 'TJK',
  'TKM', 'TLS', 'TOG', 'TPE', 'TRI', 'TUN', 'TUR', 'TUV', 'UAE', 'UGA', 'UKR',
  'URU', 'USA', 'UZB', 'VAN', 'VEN', 'VIE', 'VIN', 'YEM', 'ZAM', 'ZIM'];
FlagIconFac.IMAGE_URL = 'http://www.google.com/images/flags/all.png';

FlagIconFac.prototype.getIcon = function(name, asHtml, styleText){
	return MakeImage(FlagIconFac.COUNTRIES_ICON_MAP, name,
		 24, 14, 15, FlagIconFac.IMAGE_URL, asHtml, styleText);
}

function SportIconFac() {
  SportIconFac.SPORTS_ICON_MAP = {};
  for (var i = 0; i < SportIconFac.SPORTS.length; i++) {
    SportIconFac.SPORTS_ICON_MAP[SportIconFac.SPORTS[i]] = i;
  }
}
SportIconFac.SPORTS = ['AR', 'AT', 'BB', 'BD', 'BK', 'BV', 'BX', 'CB', 'CF', 'CM',
  'CR', 'CS', 'CT', 'DV', 'EC', 'ED', 'ES', 'FB', 'FE', 'GA', 'GR', 'GT', 'HB',
  'HO', 'JU', 'MP', 'RO', 'SA', 'SH', 'SO', 'SW', 'SY', 'TE', 'TK', 'TR', 'TT',
  'VO', 'WF', 'WG', 'WL', 'WP'];
SportIconFac.IMAGE_URL = Default.imageUrl + 'img/all-20.png';

SportIconFac.prototype.getIcon = function(name, asHTML, styleText){
	return MakeImage(SportIconFac.SPORTS_ICON_MAP, name,
		 20, 20, 7, SportIconFac.IMAGE_URL, asHTML, styleText);
}

function MedalIconFac() {
  MedalIconFac.MEDALS_ICON_MAP = {};
  for (var i = 0; i < MedalIconFac.MEDALS.length; i++) {
    MedalIconFac.MEDALS_ICON_MAP[MedalIconFac.MEDALS[i]] = i;
  }
}
MedalIconFac.MEDALS = ['gold', 'silver', 'bronze', 'total'];
MedalIconFac.IMAGE_URL = Default.imageUrl + 'img/medals-stacked.gif';

MedalIconFac.prototype.getIcon = function(name, asHTML, styleText){
	if(name != "total")
	  return MakeImage(MedalIconFac.MEDALS_ICON_MAP, name,
		   16, 16, 4, MedalIconFac.IMAGE_URL, asHTML, styleText);
	else
    return MakeImage(MedalIconFac.MEDALS_ICON_MAP, name,
	     19, 20, 4, MedalIconFac.IMAGE_URL, asHTML, styleText);
}

var IconFactories = {
	flag: new FlagIconFac(),
	sport: new SportIconFac(),
	medal: new MedalIconFac()
};

/*****************************************************************/
/************ END: Sprite icons implementation *******************/
/*****************************************************************/
